package com.wwj.auction.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wwj.auction.admin.entity.UserAddress;
import com.wwj.auction.admin.entity.UserInfo;
import com.wwj.auction.admin.entity.vo.PageModel;
import com.wwj.auction.admin.entity.vo.UserInfoVo;
import com.wwj.auction.admin.mapper.UserAddressMapper;
import com.wwj.auction.admin.mapper.UserInfoMapper;
import com.wwj.auction.admin.service.UserInfoService;
import com.wwj.auction.common.util.SystemUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author wwj
 * @since 2021-08-25
 */
@Service
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> implements UserInfoService {

    @Resource
    private UserAddressMapper userAddressMapper;

    @Override
    public PageModel<UserInfoVo> listForBuyer(Integer page, Integer limit, Map<String, Object> params, Integer userType) {
        LambdaQueryWrapper<UserInfo> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(UserInfo::getUserType, userType);
        // 按id降序排序
        wrapper.orderByDesc(UserInfo::getId);
        // 检查是否携带了查询条件
        if (params != null) {
            // 若是携带了条件，则添加上查询条件（手机号、用户状态）
            if (params.containsKey("phone") || !StringUtils.isEmpty(params.get("phone"))) {
                wrapper.like(UserInfo::getPhone, params.get("phone"));
            }
            if (params.containsKey("status") || !StringUtils.isEmpty(params.get("status"))) {
                wrapper.eq(UserInfo::getStatus, params.get("status"));
            }
        }
        // 执行分页查询流程，传入当前页码和每页显示条数
        Page<UserInfo> userInfoPage = new Page<>(page, limit);
        List<UserInfo> userInfoList = baseMapper.selectPage(userInfoPage, wrapper).getRecords();
        // 封装成Vo数据
        StringBuffer sb = new StringBuffer();
        List<UserInfoVo> userInfoVoList = userInfoList.stream().map(userInfo -> {
            UserInfoVo userInfoVo = new UserInfoVo();
            BeanUtils.copyProperties(userInfo, userInfoVo);
            // 处理地址
            Integer addressId = userInfo.getAddress();
            if(addressId != null){
                // 通过地址id查询用户地址
                UserAddress userAddress = userAddressMapper.selectById(addressId);
                sb.setLength(0);
                sb.append(userAddress.getProvince());
                sb.append(userAddress.getCity());
                sb.append(userAddress.getCounty());
                userInfoVo.setAddress(sb.toString());
            }
            // 处理登录时间
            LocalDateTime loginTime = userInfo.getLoginTime();
            String time = SystemUtil.parseDate(loginTime);
            userInfoVo.setLoginTime(time);
            return userInfoVo;
        }).collect(Collectors.toList());

        // 封装最终响应结果
        PageModel<UserInfoVo> pageModel = new PageModel<>();
        pageModel.setList(userInfoVoList);
        pageModel.setTotal(userInfoPage.getTotal());
        return pageModel;
    }

    @Override
    public void lock(Integer id, Integer status) {
        // 通过id查询用户
        UserInfo userInfo = baseMapper.selectById(id);
        userInfo.setStatus(status);
        // 更新
        baseMapper.updateById(userInfo);
    }

    @Override
    public void lockStore(Integer id, Integer status) {
        // 通过id查询用户
        UserInfo userInfo = baseMapper.selectById(id);
        // 若是解封，则直接将店铺状态设置为未开业
        userInfo.setStoreStatus(status);
        // 更新
        baseMapper.updateById(userInfo);
    }
}
